﻿using Learun.Util;
using Learun.Util.SqlSugar;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using DbType = System.Data.DbType;

namespace Learun.Application.Base.SystemModule
{
    #region MyRegion

    #endregion
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创建人：研发部
    /// 日 期：2017.03.08
    /// 描 述：附件管理
    /// </summary>
    public class AnnexesFileService
    {
        #region 仓储
        Repository<AnnexesFileEntity> _annexesFileRepository => new Repository<AnnexesFileEntity>();
        #endregion
        #region 属性 构造函数
        private string fieldSql;
        public AnnexesFileService()
        {
            fieldSql = @" 
                   t.F_Id,
                   t.F_FolderId,
                   t.F_FileName,
                   t.F_FilePath,
                   t.F_FileSize,
                   t.F_FileExtensions,
                   t.F_FileType,
                   t.F_DownloadCount,
                   t.F_CreateDate,
                   t.F_CreateUserId,
                   t.F_CreateUserName,
                   t.F_WebFileID,
                   t.F_ThumbnailImgPath
                    ";
        }
        #endregion


        #region 获取数据
        /// <summary>
        /// 获取实体列表
        /// </summary>
        /// <param name="folderId">主键值串</param>
        /// <returns></returns>
        public IEnumerable<AnnexesFileEntity> GetList(string folderId)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("SELECT " + fieldSql + " FROM LR_Base_AnnexesFile t WHERE t.F_FolderId = (@folderId) Order By t.F_CreateDate ");
                return SqlSugarHelper.Db.SqlQueryable<AnnexesFileEntity>(strSql.ToString()).AddParameters(new { folderId = folderId }).ToList();
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 获取实体(根据F_Id)
        /// </summary>
        /// <param name="F_Id">主键值串</param>
        /// <returns></returns>
        public IEnumerable<AnnexesFileEntity> GetAnnexesFileEntity(string F_Id)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("SELECT " + fieldSql + " FROM LR_Base_AnnexesFile t WHERE t.F_Id = (@F_Id) Order By t.F_CreateDate ");
                //return this.BaseRepository().FindList<AnnexesFileEntity>(strSql.ToString(), new { F_Id = F_Id });
                return SqlSugarHelper.Db.SqlQueryable<AnnexesFileEntity>(strSql.ToString()).AddParameters(new { F_Id = F_Id }).ToList();
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 获取附件名称集合
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <returns></returns>
        public string GetFileNames(string keyValue)
        {
            try
            {
                string res = "";
                IEnumerable<AnnexesFileEntity> list = GetList(keyValue);
                foreach (var item in list)
                {
                    if (!string.IsNullOrEmpty(res))
                    {
                        res += ",";
                    }
                    res += item.F_FileName;
                }
                return res;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        /// <summary>
        /// 获取附件
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <returns></returns>
        public IEnumerable<AnnexesFileEntity> GetEntitys(string keyValue)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("SELECT " + fieldSql + " FROM LR_Base_AnnexesFile t WHERE t.F_FolderId = (@folderId) Order By t.F_CreateDate DESC  ");
                //return this.BaseRepository().FindList<AnnexesFileEntity>(strSql.ToString(), new { folderId = keyValue });
                return SqlSugarHelper.Db.SqlQueryable<AnnexesFileEntity>(strSql.ToString()).AddParameters(new { folderId = keyValue }).ToList();

            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        /// <summary>
        /// 获取附件实体
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <returns></returns>
        public AnnexesFileEntity GetEntity(string keyValue)
        {
            try
            {
                var fileEntity = _annexesFileRepository.GetById(keyValue);
                if (fileEntity == null)
                {
                    IEnumerable<AnnexesFileEntity> fileList = GetList(keyValue);
                    foreach (var item in fileList)
                    {
                        return item;
                    }
                }
                return fileEntity;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 获取附件实体
        /// </summary>
        /// <param name="folderId">附件key</param>
        /// <returns></returns>
        public AnnexesFileEntity GetByfolderIdEntity(string folderId)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("SELECT " + fieldSql + " FROM LR_Base_AnnexesFile t WHERE t.F_FolderId = (@folderId) Order By t.F_CreateDate ");
                //return this.BaseRepository().FindEntity<AnnexesFileEntity>(strSql.ToString(), new { folderId = folderId });
                return SqlSugarHelper.Db.Ado.SqlQuerySingle<AnnexesFileEntity>(strSql.ToString(), new { folderId = folderId });
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }


        /// <summary>
        /// 获取实体列表(根据F_Id)
        /// </summary>
        /// <param name="F_Id">主键</param>
        /// <returns></returns>
        public AnnexesFileEntity GetEntityByF_Id(string F_Id)
        {
            try
            {
                var fileEntity = _annexesFileRepository.GetById(F_Id);
                if (fileEntity == null)
                {
                    IEnumerable<AnnexesFileEntity> fileList = GetAnnexesFileEntity(F_Id);
                    foreach (var item in fileList)
                    {
                        return item;
                    }
                }
                return fileEntity;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 获取页面显示列表数据
        /// </summary>
        /// <param name="pagination">分页信息</param>
        /// <param name="queryJson">查询参数</param>
        /// <returns></returns>
        public IEnumerable<AnnexesFileEntity> GetPageList(Pagination pagination, string queryJson)
        {
            try
            {
                var strSql = new StringBuilder();
                strSql.Append("SELECT ");
                strSql.Append(@"
                t.F_Id,
                t.F_WebFileID,
                t.F_FolderId,
                t.F_FileName,
                t.F_CreateDate,
                t.F_CreateUserName
                ");
                strSql.Append("  FROM LR_Base_AnnexesFile t ");
                strSql.Append("  WHERE 1=1 ");
                var queryParam = queryJson.ToJObject();
                // 虚拟参数
                //var dp = new DynamicParameters(new { });
                var dp = new List<SugarParameter>();
                if (!queryParam["F_FolderId"].IsEmpty())
                {
                    dp.Add(new SugarParameter("F_FolderId", queryParam["F_FolderId"].ToString(), DbType.String));
                    strSql.Append(" AND t.F_FolderId = @F_FolderId");
                }
                //return this.BaseRepository().FindList<AnnexesFileEntity>(strSql.ToString(), dp, pagination);
                return SqlSugarHelper.Db.SqlQueryable<AnnexesFileEntity>(strSql.ToString()).AddParameters(dp).ToPageList(pagination.page, pagination.rows);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 保存数据实体
        /// </summary>
        /// <param name="folderId">附件夹主键</param>
        /// <param name="annexesFileEntity">附件实体数据</param>
        public void SaveEntity(string folderId, AnnexesFileEntity annexesFileEntity)
        {
            try
            {
                annexesFileEntity.Create();
                annexesFileEntity.F_FolderId = folderId;
                _annexesFileRepository.Insert(annexesFileEntity);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 修改实体
        /// <param name="annexesFileEntity">实体</param>
        /// </summary>
        public void EditEntity(AnnexesFileEntity annexesFileEntity)
        {
            try
            {
                _annexesFileRepository.Update(annexesFileEntity);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 修改集合
        /// <param name="annexesFileList">集合</param>
        /// </summary>
        public void MuliteEditEntity(List<AnnexesFileEntity> annexesFileList)
        {
            try
            {
                _annexesFileRepository.UpdateRange(annexesFileList);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 删除附件
        /// </summary>
        /// <param name="fileId">文件主键</param>
        /// <param name="folderId">文件夹主键</param>
        public void DeleteEntity(string fileId)
        {
            try
            {
                _annexesFileRepository.Delete(new AnnexesFileEntity() { F_Id = fileId });
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        #endregion
    }
}
